﻿using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using WLYT.Application.Interfaces;
using WLYT.Domain.Entries;

namespace WLYT.WMS.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class Sys_WarehouseController(IBaseService<Sys_Warehouse> sysWarehouseService) : ControllerBase
    {
        [HttpGet]
        public async Task<List<Sys_Warehouse>> GetData()
        {
            return await sysWarehouseService.GetListAsync(true);
        }

        [HttpPost]
        public async Task<bool> Save(Sys_Warehouse model)
        {
            sysWarehouseService.AsSugarClient().DataCache.RemoveDataCache("Sys_Warehouse");
            var query = await sysWarehouseService.GetByIdAsync(model.Id);
            if (query == null)
            {
                return await sysWarehouseService.InsertAsync(model);
            }
            else
            {
                return await sysWarehouseService.UpdateAsync(model);
            }
        }

        [HttpPost]
        public async Task<bool> Remove(int id)
        {
            sysWarehouseService.AsSugarClient().DataCache.RemoveDataCache("Sys_Warehouse");
            return await sysWarehouseService.DeleteAsync(id);
        }
    }
}
